<?php
/**
 * 探霸websocket服务
 */
namespace Core;

class WSServer extends \Swoole\WebSocket\Server
{

    /**
     * 构造方法
     * @param string $ip 绑定IP
     * @param int $port 监听端口
     */
    public function __construct($ip, $port)
    {
        parent::__construct($ip, $port);
    }

    /**
     * 启动服务器方法
     * @param  Handler $handler 处理对象
     * @return bool
     */
    public function run($handler)
    {
        $this->set(config('server'));

        $this->on('start', array($handler, 'onStart'));                 //服务器启动
        $this->on('ManagerStart', array($handler, 'onManagerStart'));   //管理进程启动
        $this->on('WorkerStart', array($handler, 'onWorkerStart'));     //worker进程启动
        $this->on('open', array($handler, 'onOpen'));                   //客户端连接
        $this->on('message', array($handler, 'onMessage'));             //接收到客户端发送消息
        $this->on('close', array($handler, 'onClose'));                 //客户端管链接
        $this->on('WorkerStop', array($handler, 'onWorkerStop'));       //worker进程启动
        $this->on('ManagerStop', array($handler, 'onManagerStop'));     //管理进程停止
        $this->on('Task', array($handler, 'onTask'));
        $this->on('Finish', array($handler, 'onFinish'));

        $argv = getopt('s:h:p:u:', array("oh:", "op:"));
        if($argv['s'] == 'halert')$this->on('pipeMessage', array($handler, 'onPipeMessage'));

        if (method_exists($handler, 'onShutdown')) {
            $this->on('shutdown', array($handler, 'onShutdown'));
        }

        if (method_exists($handler, 'getProcess')) {
            if ($process = $handler->getProcess($this)) {
                foreach ($process as $p) {
                    $this->addProcess($p);
                }
            }
        }
        $this->start();
    }
}